package com.example.buyticketlishi.wightview;

import android.content.Context;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.example.baselibrary.R;
import com.example.baselibrary.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.ToastUtil;


public class NumberSubAddView extends LinearLayout implements View.OnClickListener, TextWatcher {

    private static final String TAG = "AmountView";

    private int amount = 0; //数量


    private OnAmountChangeListener mListener; //数量变化的回调接口

    private EditText etAmount;//数量

    private ImageView btnDecrease;//-按钮

    private ImageView btnIncrease;//+按钮

    private Context context;

    private int max = 1000;

    private int min = 0;

    public NumberSubAddView(Context context) {
        this(context, null);
        this.context = context;
    }

    public NumberSubAddView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.model_count_views, this);
        btnDecrease = findViewById(R.id.iv_count_minus);
        btnIncrease = findViewById(R.id.iv_count_add);
        etAmount = findViewById(R.id.et_count);

        btnDecrease.setOnClickListener(this);
        btnIncrease.setOnClickListener(this);
        etAmount.addTextChangedListener(this);
    }

    public void setAmountValue(int amount) {
        this.amount = amount;
        etAmount.setText(amount + "");
    }

    public void setMinValue(int amount) {
        this.min = amount;
        etAmount.setText(amount + "");
    }

    public void setMaxValue(int amount) {
        this.max = amount;
//        etAmount.setText(amount + "");
    }

    public String getValue() {
        return etAmount.getText().toString().trim();
    }

    //数量变化的回调接口
    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.mListener = onAmountChangeListener;
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().isEmpty()) {
            return;
        }


    }

    @Override
    public void onClick(View v) {
        int id = v.getId();//如果是减法
        if (id == R.id.iv_count_minus) {
            if (amount > min) {
                amount--;
            } else {
//                ToastUtil.showToast("最小票数为" + min);
                SelfToastUtil.getInstance().show("最小票数为" + min);
            }
            etAmount.setText(amount + "");
            //如果是加法
        } else if (id == R.id.iv_count_add) {
            if (max <= amount) {
//                ToastUtil.showToast("最大票数为" + max);
                SelfToastUtil.getInstance().show("最大票数为" + max);
            } else {
                amount++;
            }
            etAmount.setText(amount + "");
        }
        etAmount.clearFocus();
        if (mListener != null) {
            mListener.onAmountChange(this, amount);
        }

    }


    public interface OnAmountChangeListener {
        void onAmountChange(View view, int amount);
    }
}
